﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace MDIPad
{
    public partial class Form1 : Form
    {
        string filePath;
        EditForm current;
        RichTextBox RichTB;
        public Form1()
        {
            InitializeComponent();

            newToolStripMenuItem.Click += NewToolStripMenuItem_Click;
            openToolStripMenuItem.Click += OpenToolStripMenuItem_Click;
            saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
            saveAsToolStripMenuItem.Click += SaveAsToolStripMenuItem_Click;
            copyToolStripMenuItem.Click += CopyToolStripMenuItem_Click;
            pasteToolStripMenuItem.Click += PasteToolStripMenuItem_Click;
            cutToolStripMenuItem.Click += CutToolStripMenuItem_Click;
            selectAllToolStripMenuItem.Click += SelectAllToolStripMenuItem_Click;
            colorStripMenuItem.Click += ColorStripMenuItem_Click;
            backgroundColorMenuItem.Click += BackgroundColorMenuItem_Click;
            fontStripMenuItem.Click += FontStripMenuItem_Click;
            exitToolStripMenuItem.Click += ExitToolStripMenuItem_Click;

        }

        private void FontStripMenuItem_Click(object sender, EventArgs e)
        {
            if (RichTB != null)
            {
                if(RichTB.SelectionFont == null)
                {
                    RichTB.SelectionFont = new Font(FontFamily.GenericMonospace, 12, FontStyle.Regular);
                }
                else
                {
                    RichTB.SelectionFont = new Font(RichTB.SelectionFont.FontFamily, RichTB.SelectionFont.Size + 2, RichTB.SelectionFont.Style);
                }
            }
        }

        private void BackgroundColorMenuItem_Click(object sender, EventArgs e)
        {
            if(RichTB != null)
            {
                RichTB.SelectionBackColor = Color.Azure;
            }
        }

        private void ColorStripMenuItem_Click(object sender, EventArgs e)
        {
            if(RichTB != null)
            {
                RichTB.SelectionColor = Color.Red;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(saveFileDialog1.ShowDialog()== DialogResult.OK)
            {
                filePath = saveFileDialog1.FileName;
                RichTB.SaveFile(filePath);
            }
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void SelectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RichTB.SelectAll();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int start = RichTB.SelectionStart;
            RichTB.Text = RichTB.Text.Remove(start, RichTB.SelectionLength);
            RichTB.SelectionStart = start;
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string temp = Clipboard.GetText();
            if (!string.IsNullOrEmpty(temp))
            {
                int index = RichTB.SelectionStart;
                string text = RichTB.Text;
                text = text.Remove(RichTB.SelectionStart, RichTB.SelectionLength);
                text = text.Insert(RichTB.SelectionStart, temp);
                RichTB.Text = text;
                RichTB.SelectionStart = index;
            }
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(RichTB.SelectedText))
            {
                Clipboard.SetText(RichTB.SelectedText);
            }
        }

        private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                SaveAsToolStripMenuItem_Click(sender, e);
            }
            else
            {
                RichTB.SaveFile(filePath);
            }
        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                current = new EditForm { Text = openFileDialog1.SafeFileName, MdiParent = this };
                RichTB = current.richTextBox1;
                current.Show();
                filePath = openFileDialog1.FileName;
                if(Path.GetExtension(filePath) == ".txt")
                {
                    RichTB.LoadFile(filePath, RichTextBoxStreamType.PlainText);
                }
                else
                {
                    RichTB.LoadFile(filePath);
                }
                
            }
        }

        private void NewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            current = new EditForm
            {
                MdiParent = this
            };
            current.Show();
            RichTB = current.richTextBox1;
            //richTextBox1.Text = string.Empty;
            //filePath = null;
        }
    }
}
